"use strict";
import router from '../router'
import Vue from 'vue';
import axios from "axios";
import { Message } from 'element-ui';
// Full config:  https://github.com/axios/axios#request-config
// axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
let baseUrl
if (process.env.NODE_ENV === "development") {
    baseUrl = '/api'
} else {
    // baseUrl =  'http://106.52.251.232/zrht_extrapolation/zrht'
    // baseUrl = 'https://www.yoyocare.com.cn/zrht_extrapolation/zrht'
    baseUrl= 'http://119.29.143.61/interview-service/'//新测试
}
let config = {
    baseURL:baseUrl,//测试
    // baseURL:'https://www.zrpas.com.cn//pas_manage/pas/',//// 正式
  // baseURL: process.env.baseURL || process.env.apiUrl || ""
  timeout: 60 * 1000, // Timeouts

  headers:{
      // 'token':  window.localStorage.getItem("token"),e
    // 'Content-Type':'application/json'
    //   'Content-Type': 'application/x-www-form-urlencoded'
  },
    withCredentials: true,//允许跨域携带cookie信息
    crossDomain: true,
  // withCredentials: true, // Check cross-site Access-Control
};

const _axios = axios.create(config);

_axios.interceptors.request.use(

  function(config) {
    // // Do something before request is sent
      let token = window.localStorage.getItem("token")
      if (token) {
          config.headers.token = token;    //将token放到请求头发送给服务器
          return config;
          //这里经常搭配token使用，将token值配置到tokenkey中，将tokenkey放在请求头中
          // config.headers['token'] = Token;
      }
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);

// Add a response interceptor
_axios.interceptors.response.use(
  function(response) {
      console.log(response)
      if (response.status==500){
          Message.error("登录失效，请重新登陆");
          localStorage.removeItem('role1',);
          localStorage.removeItem('resourcesrList',);
          localStorage.removeItem('btnPermission',);

          router.push({
                  path: '/login',
              })
          return
      }
//拦截器里删除掉登录时的缓存
    //   if (response.data == ''){
    //     Message.error("登录失效，请重新登陆")
    //       localStorage.removeItem('role',);
    //       localStorage.removeItem('ROUTER',);
    //       localStorage.removeItem('SET_ROLEID',);
    //       localStorage.removeItem('SET_AGENCY',)
    //       setTimeout(function(){
    //         router.push({
    //           path: '/login',
    //       })
    //       }, 1000)
    //       return
    //   }
    // Do something with response data

    return response;
  },
  function(error) {
    // Do something with response error
    return Promise.reject(error);
  }

);

Plugin  .install = function(Vue, options) {
  Vue.axios = _axios;
  window.axios = _axios;
  Object.defineProperties(Vue.prototype, {
    axios: {
      get() {
        return _axios;
      }
    },
    $axios: {
      get() {
        return _axios;9
      }
    },
  });
};

Vue.use(Plugin)

export default Plugin;
